<?php
namespace Admin\Controller;
use Think\Controller;
class DealerController extends CommonController {
    public function dealer_index(){
        $where = '';
        $search_content = I('search_content');
        if($search_content){
            $where['dealer_name'] = array('like',"%$search_content%");
            $where['store_name'] = array('like',"%$search_content%");
        }
        $row = 10;
        $p = $_GET['p'] ? $_GET['p'] : 1;
        $count = M('dealer')
                ->join('AS d JOIN __USER__ AS u ON d.dealer_id = u.id')
                ->where($where)
                ->count();
        $Page = new \Think\Page($count,$row);
        $show = $Page->show();
        $list = M('dealer')
                ->field('d.*,u.phone,pr.name AS province,ci.name AS city,co.name AS county')
                ->join('AS d JOIN __USER__ AS u ON d.dealer_id = u.id')
                ->join('JOIN __REGION__ pr ON pr.id = d.province')
                ->join('JOIN __REGION__ ci ON ci.id = d.city')
                ->join('JOIN __REGION__ co ON co.id = d.county')
                ->where($where)
                ->page($p,$row)
                ->select();
        $this->assign('p',$p);
        $this->assign('search_content',$search_content);
        $this->assign('page',$show);
        $this->assign('list',$list);
        $this->display();
    }

    //编辑经销商
    public function dealer_edit(){
        //查新该经销商信息
        $info = M('dealer')->where('dealer_id='.I('uid'))->find();
        if(IS_POST){
            $id = I('uid');
            if($_FILES){
                $picture = upload();
                if($picture['store_picture']){
                    $data['store_picture'] = $picture['store_picture'];
                }
                if($picture['QR_code']){
                    $data['QR_code'] = $picture['QR_code'];
                }
            }
            $data['dealer_name'] = I('dealer_name');
            $data['dealer_area'] = I('dealer_area');
            $data['telephone'] = I('telephone');
            $data['open_time'] = I('open_time');
            $data['province'] = I('province');
            $data['city'] = I('city');
            $data['county'] = I('county');
            $data['address'] = I('address');
            $data['desc'] = I('desc');
            if(false == M('dealer')->where('dealer_id='.$id)->save($data)){
                $this->error('修改失败');
            }else{
                if($picture['store_picture']){
                    unlink(ltrim($info['store_picture'],'/'));
                }
                if($picture['QR_code']){
                    unlink(ltrim($info['QR_code'],'/'));
                }
                $this->success('修改成功',U('dealer_index',array('p'=>I('p'),'search_content'=>I('search_content'))));
            }
        }

        $this->assign('info',$info);
        //获取省级列表
        $province = M('region')->where('pid=1')->select();
        //获取所在省级的所有市级列表
        $city = M('region')->where('pid='.$info['province'])->select();
        //获取县区列表
        $county = M('region')->where('pid='.$info['city'])->select();
        $this->assign('p',I('p'));
        $this->assign('search_content',I('search_content'));
        $this->assign('province',$province);
        $this->assign('city',$city);
        $this->assign('county',$county);
        $this->display();
    }

    //删除
    public function dealer_del(){
        $id = I('uid');
        if(M('dealer')->where('dealer_id='.$id)->delete()){
            //还原用户表中的普通用户类型和非VIP状态
            if(false === M('user')->where('id='.$id)->save(array('user_type'=>1,'vip'=>0))){
                $this->error('数据异常');
            }
            //修改用户session信息
            session('user_vip',0);
            session('user_type',1);
            
            $this->success('删除成功',U('dealer_index',array('p'=>I('p'),'search_content'=>I('search_content'))));
        }else{
            $this->error('数据异常');
        }
    }

    //地区联动
    public function region(){
        $where = array(
            'pid'=>I('pid')
        );
        $list = M('region')->where($where)->select();
        echo json_encode($list);
    }
}